package com.atguigu.edu.service.impl;

import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.edu.entity.chapter.ChapterVo;
import com.atguigu.edu.entity.chapter.VideoVo;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.edu.service.VideoService;
import com.atguigu.edu.entity.Chapter;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.mapper.ChapterMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ld
 * @since 2022-08-16
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Resource
    private VideoService videoService;

    @Override
    public List<ChapterVo> getAllChapterVideo(String courseId) {
        List<ChapterVo> chapterVoList=new ArrayList<>();
        //1、根据courseId获取到所有章节
        LambdaQueryWrapper<Chapter> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(!courseId.isEmpty(),Chapter::getCourseId,courseId);
        List<Chapter> chapterList = baseMapper.selectList(queryWrapper);
        //2、将章节与小节合并
        chapterVoList=chapterList.stream().map(
                item->{
                    ChapterVo chapterVo=new ChapterVo();
                    BeanUtils.copyProperties(item,chapterVo);
                    LambdaQueryWrapper<Video> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(!courseId.isEmpty(),Video::getCourseId,courseId);
                    List<Video> videoList = videoService.list(lambdaQueryWrapper);
                    List<VideoVo> videoVoList=new ArrayList<>();
                    for (Video video: videoList){
                        VideoVo videoVo=new VideoVo();
                        if(video.getChapterId().equals(item.getId())) {
                            BeanUtils.copyProperties(video, videoVo);
                            videoVoList.add(videoVo);
                        }
                    }
                    chapterVo.setChildren(videoVoList);
                    return chapterVo;
                }
        ).collect(Collectors.toList());
        return chapterVoList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        LambdaQueryWrapper<Video> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getChapterId,chapterId);
        int count = videoService.count(queryWrapper);
        if(count>0){
            throw new CustomException(20001,"不能删除");
        }else{
            int result=baseMapper.deleteById(chapterId);
            return result>0;
        }

    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        LambdaQueryWrapper<Chapter> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Chapter::getCourseId,courseId);
        baseMapper.delete(queryWrapper);
    }
}
